package com.wuming.house.util;

import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.Map;

/**
 * Http请求工具类
 *
 * @author hanfei
 * @Title :HttpRequestUtil
 * @ProjectName : drivermanage
 * @Description:
 * @date 18/7/27上午10:56
 */

public class HttpRequestUtil {
    public static String httpPost(String url, Map<String, String> map, String cookie) throws IOException {
        //获取请求连接
        Connection con = Jsoup.connect(url);
        //遍历生成参数
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                //添加参数
                con.data(entry.getKey(), entry.getValue());
            }
        }
        //插入cookie（头文件形式）
        con.header("Cookie", cookie);
        Document doc = con.post();
        System.out.println(doc);
        return doc.toString();
    }

    public static Map<String, String> getCookies(String url) throws IOException {
        Connection con = Jsoup.connect(url);
        Connection.Response resp = con.method(Connection.Method.POST).execute();
        //获取cookie名称为__bsi的值
        Map<String, String> cookies = resp.cookies();
        System.out.println("请求的cookies==============" + cookies);
        return cookies;
    }

    public static void main(String[] args) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt("17788911056", "e27d6136eedcd80c7337ede57ea80c9ae27d6136eed", "wx63aeaca36da3cc1d");

        String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
        String fromXML = String.format(format, "N/zeiv3gih2qUCFFxYHKm9899qQVt/DLUKSTiIu1PRjcQgynmkVq3j+Q0kqvfX62NtKgHIpO9yB4LIWROM146aFgKwn3G6TExDwZ6LbN0tnyjhrZzOSCoFDeDgzXMVoZYOe2ZuX68QjE+ESXXaMYJkR1GK2A2vm2pP9u+T1Xne5CBDbwU4MZJbB3J+oXKqN3zh/KOoCEz2I5K3xVSxKPwnjx531+YtpsJe8d2ND7REiOLyDYS8dpMmDxBxp1PHm7vjxYr/Fh1HyYPZ+TqnxgLNaZPaNVYD5P3XxcVf9MdwLkzBaA5MW9WE0gt+laAQ3S6nYJclCf91NY9C5x/Rf+XUAGLVw7UI4R6UcOtrjlF8q4B3QKBb4ZVVGI+QByxWnwUOd51t7L1u32UiuBvbNFotj4eUUdPjTjHbHCq6tfdWf/Yih1F6+cwvmzPKb5dR1x1hvu0QlBXdwZY9u0eIaMWQ==");

        String msgContent = pc.decryptMsg("1960c9dc8836e31675c80831981b2982ecca3cb2", "1539225454", "1205204222", fromXML);

        System.out.println("解密后明文: " + msgContent);
    }
}
